package com.cy.myblog.dao.impl;

import com.cy.myblog.dao.BaseDaoI;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T>{

	private SessionFactory sessionFactory;
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Serializable save(T o) {
		if(o != null)
		   return getSession().save(o);
		return null;
	}

	public void delete(T o) {
		if(o != null)
		   getSession().delete(o);
	}

	public void update(T o) {
		if(o != null)
		   getSession().update(o);
	}

	public void saveOrUpdate(T o) {
		if(o != null)
		   getSession().saveOrUpdate(o);
	}

	@SuppressWarnings("unchecked")
	public T getById(Class<T> clazz, Serializable id) {
		return (T) getSession().get(clazz, id); 
	}

	@SuppressWarnings("unchecked")
	public T getByHql(String hql) {
		Query query = getSession().createQuery(hql);
		List<T> list=query.list();
		if(!list.isEmpty() && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	public T getByHql(String hql, Map<String, Object> params) {
		Query query = getSession().createQuery(hql);
		if(params != null && !params.isEmpty()){
			for(String key : params.keySet()){
				query.setParameter(key, params.get(key));
			}
			return (T) query.uniqueResult();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<T> find(String hql) {
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	public List<T> find(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> find(String hql, int page, int rows) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> find(String hql, Map<String, Object> params, int page,
			int rows) {
		// TODO Auto-generated method stub
		return null;
	}

	public Long count(String hql) {
		// TODO Auto-generated method stub
		return null;
	}

	public Long count(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}

	public int executeHql(String hql) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int executeHql(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<Map> findBySql(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Map> findBySql(String sql, int page, int rows) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Map> findBySql(String sql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Map> findBySql(String sql, Map<String, Object> params,
			int page, int rows) {
		// TODO Auto-generated method stub
		return null;
	}

	public int executeSql(String sql) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int executeSql(String sql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return 0;
	}

	public BigInteger countBySql(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	public BigInteger countBySql(String sql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}

}
